package com.foton.bpc.all.register.cluster.loadbalance.impl;

import com.foton.bpc.all.register.cluster.loadbalance.LoadBalance;
import com.foton.bpc.all.remoting.invoker.Invoker;

import java.net.URI;
import java.util.Map;
import java.util.Random;

/**
 * @Author RenPu
 * @Date 2023/6/13 16:06
 * @Version 1.0
 * @Description: 负载均衡---随机策略得实现
 **/
public class RandomLoadBalance implements LoadBalance {


    @Override
    public Invoker select(Map<URI, Invoker> invokerMap) {

        //根据map中value得个数，进行随机获取一个
        int index = new Random().nextInt(invokerMap.values().size());

        return invokerMap.values().toArray(new Invoker[]{})[index];
    }
}
